package com.work.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.work.bean.PestsBean;
import com.work.mapper.PestsMapper;
import com.work.service.IPestsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.crypto.interfaces.PBEKey;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Controller
@RequestMapping("/pests")
public class PestsController {
    @Resource
    private IPestsService pestsService;

    private PestsBean addPest;

    public PestsController() {
        addPest = new PestsBean();
        addPest.setPestsLarvaPic("default.jpg");
        addPest.setPestsAdultPic("default.jpg");
    }

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    //show
    @RequestMapping("/show")
    @ResponseBody
    public IPage<PestsBean> findByItem(Integer pageNo,Integer pageSize,String pestsName,String pestsHost){
        System.out.println(pestsService.findByItem(pageNo, pageSize, pestsName, pestsHost));
        return pestsService.findByItem(pageNo,pageSize,pestsName,pestsHost);
    }

    //add
    @RequestMapping("/addPest")
    @ResponseBody
    public String addPest(PestsBean pest){
        addPest.setPestsDamage(pest.getPestsDamage());
        addPest.setPestsHost(pest.getPestsHost());
        addPest.setPestsReproduction(pest.getPestsReproduction());
        addPest.setPestsEnemy(pest.getPestsEnemy());
        addPest.setPestsPrevention(pest.getPestsPrevention());
        addPest.setPestsName(pest.getPestsName());

        pestsService.save(addPest);

        addPest.setPestsLarvaPic("default.jpg");
        addPest.setPestsAdultPic("default.jpg");
        return "success";
    }


    //上传成虫图片
    @RequestMapping("/pestsAdultPic")
    public void pestsAdultPic(MultipartFile file) {
        String res = uploadPic(file);
        addPest.setPestsAdultPic(res);
        System.out.println("1添加=" + addPest);

    }
    //上传幼虫图片
    @RequestMapping("/pestsLarvaPic")
    public void pestsLarvaPic(MultipartFile file) {
        String res = uploadPic(file);
        addPest.setPestsLarvaPic(res);
        System.out.println("2添加=" + addPest);
    }

    //图片上传方法
    public String uploadPic(MultipartFile file) {
        String originalName = file.getOriginalFilename();

        assert originalName != null;
        if (!originalName.endsWith(".jpg")) {
            return "error";
        }
        String format = sdf.format(new Date());
        String path = "f:\\forestVue\\forest\\src\\img";
        String realPath = path;
        System.out.println("realPath=" + realPath);
        File folder = new File(realPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String newName = UUID.randomUUID().toString() + ".jpg";
        try {
            file.transferTo(new File(folder, newName));
            return newName;
        } catch (IOException e) {
            return e.getMessage();
        }
    }



}
